package com.ningmengs789.live.ui.shopping.ui.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.R;
import com.ningmengs789.live.glide.ShopHomeGlideImageLoader;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.model.CategoryListDTO;
import com.ningmengs789.live.model.GoodslistDTO;
import com.ningmengs789.live.model.ShoppingHomeBean;
import com.ningmengs789.live.ui.adapter.DiscountGoodsAdapter;
import com.ningmengs789.live.ui.adapter.IntegralShoppingAdapter;
import com.ningmengs789.live.ui.adapter.ShoppingHomeAdapter;
import com.ningmengs789.live.ui.widget.TabFragmentAdapter;
import com.ningmengs789.live.utils.JsonUtil;
import com.ningmengs789.live.ui.fragment.BaseFragment;
import com.ningmengs789.live.ui.shopping.api.ShoppingApi;
import com.youth.banner_lod.Banner;
import com.youth.banner_lod.BannerConfig;
import com.youth.banner_lod.listener.OnBannerListener;
import com.youth.banner_lod.transformer.AccordionTransformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者 : Administrator (yzhg)
 * 时间 : 2021-09-07 14:27
 * 包名 : com.ningmengs789.live.ui.shopping.ui.fragment
 * 备注：商城首页Fragment
 */
public class ShoppingHomeFragment extends BaseFragment implements OnBannerListener {


    /**
     * 轮播图
     */
    @BindView(R.id.banner_shopping_home_layout)
    Banner bannerShoppingHomeLayout;

    /**
     * 首页导航列表
     */
    @BindView(R.id.recycle_navigation_home_shopping)
    RecyclerView recycleNavigationHomeShopping;

    /**
     * 积分商城 查看全部
     */
    @BindView(R.id.tv_check_all)
    TextView tvCheckAll;

    /**
     * 积分列表
     */
    @BindView(R.id.recycle_integral_shopping)
    RecyclerView recycleIntegralShopping;


    /**
     * 折扣商品全部商品
     *
     * @return
     */
    @BindView(R.id.tv_all_goods)
    TextView tvAllGoods;

    /**
     * 折扣商品列表
     */
    @BindView(R.id.recycle_limit_home)
    RecyclerView recycleLimitHome;

    /**
     * viewPager
     */
    @BindView(R.id.vp_shopping_home)
    ViewPager vpShoppingHome;

    /**
     * TabLayout
     */
    @BindView(R.id.tab_shopping_home)
    TabLayout tabShoppingHome;

    /**
     * tabLayout 标题
     */
    private List<String> tabTitle = new ArrayList<>();

    /**
     * viewPager Fragment
     */
    private List<Fragment> fragments = new ArrayList<>();


    @Override
    protected int setContentViewById() {
        return R.layout.fragment_home_shopping;
    }

    @Override
    protected void initAction() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void initData() {
        //请求网络数据
        OKHttpUtils.getInstance().getRequest(ShoppingApi.SHOPPING_HOME_DATA, "", new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                Log.i("=======失败的方法======", "onError: 走到了失败的方法" + errInfo);
            }

            @Override
            public void onSuccess(String result) {
                //获取商城首页数据成功
                ShoppingHomeBean shoppingHomeBean = JsonUtil.parseJsonToBean(result, ShoppingHomeBean.class);
                //获取轮播图数据
                List<CategoryListDTO> categoryList = shoppingHomeBean.getCategoryList();
                //设置轮播图数据
                setBannerCateGory(categoryList);


                //设置导航数据
                setNavigationData(categoryList);


                //获取积分商城
                List<GoodslistDTO> jifenlist = shoppingHomeBean.getJifenlist();
                //设置积分商城
                setIntegralShopping(jifenlist);


                //获取折扣商品
                List<GoodslistDTO> discountList = shoppingHomeBean.getDiscountList();
                //设置折扣商品
                setDiscountList(discountList);


                //设置TabLayout标题
                setTabTitle(categoryList);

                //设置ViewPager
                setViewPager();
            }
        });
    }


    /**
     * 设置ViewPager
     */
    private void setViewPager() {
        //设置Adapter
        TabFragmentAdapter tabFragmentAdapter = new TabFragmentAdapter(getChildFragmentManager(), fragments.toArray(new Fragment[fragments.size()]), tabTitle);

        //将Tablayout和viewpager绑定在一起
        tabShoppingHome.setupWithViewPager(vpShoppingHome);
        //设置adapter
        vpShoppingHome.setAdapter(tabFragmentAdapter);
        //设置加载数量
        vpShoppingHome.setOffscreenPageLimit(fragments.size());
    }


    /**
     * 设置tabLayout标题
     *
     * @param categoryList 标题
     */
    private void setTabTitle(List<CategoryListDTO> categoryList) {
        for (CategoryListDTO categoryListDTO : categoryList) {
            tabTitle.add(categoryListDTO.getName());
            fragments.add(GoodsListFragment.newIntent(categoryListDTO.getId()));
        }
    }

    /**
     * 设置折扣 商品
     *
     * @param discountList 折扣商品列表
     */
    private void setDiscountList(List<GoodslistDTO> discountList) {
        //设置折扣商品列表 设置横向的列表
        recycleLimitHome.setLayoutManager(new LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false));
        //设置Adapter
        DiscountGoodsAdapter discountGoodsAdapter = new DiscountGoodsAdapter(requireContext(), discountList, R.layout.item_discount_goods_home_shopping);
        recycleLimitHome.setAdapter(discountGoodsAdapter);
    }


    /**
     * 设置导航数据
     *
     * @param categoryList 导航数据
     */
    private void setNavigationData(List<CategoryListDTO> categoryList) {

        //遍历集合只取 前七条数据
        List<CategoryListDTO> showCategoryList = new ArrayList<>();
        for (CategoryListDTO categoryListDTO : categoryList) {
            if (showCategoryList.size() < 7) {
                showCategoryList.add(categoryListDTO);
            }
        }

        //增加更多的配置
        CategoryListDTO categoryListDTO = new CategoryListDTO();
        categoryListDTO.setId(-1);
        showCategoryList.add(categoryListDTO);

        //设置RecycleView
        recycleNavigationHomeShopping.setLayoutManager(new GridLayoutManager(requireContext(), 4));
        //设置列表适配器
        ShoppingHomeAdapter shoppingHomeAdapter = new ShoppingHomeAdapter(requireContext(), showCategoryList, R.layout.item_navigation_home_shopping);
        //配置适配器
        recycleNavigationHomeShopping.setAdapter(shoppingHomeAdapter);
    }

    /**
     * 积分商城
     *
     * @param jifenlist 积分商城数据
     */
    private void setIntegralShopping(List<GoodslistDTO> jifenlist) {
        recycleIntegralShopping.setLayoutManager(new GridLayoutManager(requireContext(), 3));
        //适配器
        IntegralShoppingAdapter integralShoppingAdapter = new IntegralShoppingAdapter(requireContext(), jifenlist, R.layout.item_integral_home_shopping);
        //设置适配器
        recycleIntegralShopping.setAdapter(integralShoppingAdapter);
    }


    /**
     * 设置轮播图数据
     *
     * @param categoryList 轮播图数据
     */
    private void setBannerCateGory(List<CategoryListDTO> categoryList) {
        //生成轮播图片集合

        List<String> images = new ArrayList<>();
        for (CategoryListDTO categoryListDTO : categoryList) {
            images.add(categoryListDTO.getImage());
        }
        bannerShoppingHomeLayout.setImages(images)
                //.setBannerTitles(App.titles)//标
                .setBannerAnimation(AccordionTransformer.class)//动画
                .setIndicatorGravity(BannerConfig.CENTER)//指示器位置
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)//指示器样式
                .setDelayTime(3000)//延迟4S
                .setImageLoader(new ShopHomeGlideImageLoader())
                .setOnBannerListener(this)
                .start();
    }

    /**
     * 轮播图点击事件
     *
     * @param position 点击第几个条目
     */
    @Override
    public void OnBannerClick(int position) {

    }
}
